
package crm.salesOrderProcessing.managedbean;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import crm.salesOrderProcessing.entity.LineItemEntity;
import crm.salesOrderProcessing.entity.SalesQuotationEntity;
import crm.salesOrderProcessing.session.SalesQuotationSessionBean;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.event.ActionEvent;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

@ManagedBean
@ViewScoped
public class CreateQuotationResultManagedBean implements Serializable{
    
    private SalesQuotationEntity salesQuotation;
    private List<LineItemEntity> lineItems;
    private Long salesQuotationId;
    private String message; 
    private double userDiscount;
    private double totalPrice;
    
    @EJB
    SalesQuotationSessionBean quotationSessionBean;

    /** Creates a new instance of CreateInquiryResultManagedBean */
    public CreateQuotationResultManagedBean() {
        quotationSessionBean = new SalesQuotationSessionBean();
    }
    
    public void initView(PhaseEvent event) {
        if (salesQuotation == null) {
            
           salesQuotation = (SalesQuotationEntity) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("newQuotation");
        }
        setLineItems(salesQuotation.getQuotationLineItems());
        setSalesQuotationId(salesQuotation.getId());
        setUserDiscount(salesQuotation.getUserDiscount());
        setTotalPrice(salesQuotation.getTotalPrice());
        message = "Sales Quotation has been saved. ID: "+String.valueOf(salesQuotationId);
     }

    public void editInquiry() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("quotation", salesQuotation);
        FacesContext.getCurrentInstance().getExternalContext().redirect("editSalesQuotation.xhtml");
    }
    
    public void sendEmail(ActionEvent event){
        String path = FacesContext.getCurrentInstance().getExternalContext().getRealPath("jasperreports");
        exportToPDFBySQLConn(path);
        quotationSessionBean.sendSalesQuotation(salesQuotation.getCompany().getEmail(), salesQuotation, path);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Email has been sent.", ""));  
    }
    
    public void goBack() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteSalesQuotation.xhtml");
    }
    
    public void goBack2() throws IOException{
        FacesContext.getCurrentInstance().getExternalContext().redirect("./deleteSalesQuotation.xhtml");
    }

    public SalesQuotationEntity getSalesQuotation() {
        return salesQuotation;
    }

    public void setQuotation(SalesQuotationEntity quotaiton) {
        this.salesQuotation = quotaiton;
    }

    public List<LineItemEntity> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItemEntity> lineItems) {
        this.lineItems = lineItems;
    }

    public Long getSalesQuotationId() {
        return salesQuotationId;
    }

    public void setSalesQuotationId(Long salesQuotationId) {
        this.salesQuotationId = salesQuotationId;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public double getUserDiscount() {
        return userDiscount;
    }

    public void setUserDiscount(double userDiscount) {
        this.userDiscount = userDiscount;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    public void exportToPDFBySQLConn(String path){
        try {

            List<LineItemEntity> lineItemList;
            lineItemList = salesQuotation.getQuotationLineItems();
            
            JRBeanCollectionDataSource jrResultSetDataSource = new JRBeanCollectionDataSource(lineItemList);
            
            HashMap parameters = new HashMap();
            parameters.put("IMAGEPATH", "http://localhost:8080/MerlionFoodERP-war/jasperreports/wood.jpg");
            
            String fileName = path+"/lineItem.jasper";
            String outFileName = path+"/SalesQuotation_"+salesQuotation.getId()+".pdf";
            // Fill the report using an empty data source
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, jrResultSetDataSource);
            
            // Create a PDF exporter
            JRExporter exporter = new JRPdfExporter();
            
            // Configure the exporter (set output file name and print object)
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            
            // Export the PDF file
            exporter.exportReport();
        } 
        catch (JRException jrex){
            System.out.println("********** Jasperreport Exception");
            jrex.printStackTrace();
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }
    
}
